
#include <cmsis_os.h>                                           // CMSIS RTOS header file
#include "main.h"

#define MAX_TICK_COUNTER_VAL 200000

/*----------------------------------------------------------------------------
 *      variable Declaration 			
 *---------------------------------------------------------------------------*/
 extern ADC_HandleTypeDef hadc1;
 extern ADC_HandleTypeDef hadc2;
 uint32_t uwTick;
/*----------------------------------------------------------------------------
 *      variable Declaration End 			
 *---------------------------------------------------------------------------*/ 

/*----------------------------------------------------------------------------
 *      Timer Declaration End			
 *---------------------------------------------------------------------------*/
 
/*----------------------------------------------------------------------------
 *      @brief Timer 1 'appHeartbeat': produce a heartbeat
 *---------------------------------------------------------------------------*/
 
void appHeartbeat (void const *argument);                             // callback function
osTimerId tmidHeartbeat;                                          // timer id
osTimerDef (tmHeartbeat, appHeartbeat);                   // timer object

/*----------------------------------------------------------------------------
 *      @brief Timer 1 'appHeartbeat': produce a heartbeat
 *---------------------------------------------------------------------------*/
 
void appTick (void const *argument);                             // callback function
osTimerId tmidTick;                                          // timer id
osTimerDef (tmTick, appTick);                   // timer object

/*----------------------------------------------------------------------------
 *      Timer Declaration End			
 *---------------------------------------------------------------------------*/
 
/*----------------------------------------------------------------------------
 *      Thread Declaration			
 *---------------------------------------------------------------------------*/
 
/*----------------------------------------------------------------------------
 *      Thread 1 'appJoystick': Readin the value of joystick and store in global structure			
 *---------------------------------------------------------------------------*/
void appJoystick(void const *argument);
osThreadId tidJoystick;
osThreadDef (appJoystick,osPriorityNormal,1,0);

/*----------------------------------------------------------------------------
 *      Thread Declaration End		
 *---------------------------------------------------------------------------*/
 
int appInitialize (void) {

  tmidHeartbeat = osTimerCreate(osTimer(tmHeartbeat),osTimerPeriodic, NULL);
  if(!tmidHeartbeat) return(-1);
	osTimerStart(tmidHeartbeat,500);

  tmidTick = osTimerCreate(osTimer(tmTick),osTimerPeriodic, NULL);
  if(!tmidTick) return(-1);
  osTimerStart(tmidTick,1);
  uwTick = 0;
	
  tidJoystick = osThreadCreate(osThread(appJoystick),NULL);
  if(!tidJoystick) return (-1);
  
  return(0);
}

void appHeartbeat (void const *argument) {
	HAL_GPIO_TogglePin(pinLED1);
}

void appTick (void const *argument){
	if(uwTick == MAX_TICK_COUNTER_VAL)
		uwTick = 0;
	else 
		uwTick++;
}

void appJoystick(void const *argument){
	volatile uint32_t result;
	while(1){
	osDelay(JOYSTICK_READ_INTERVAL);
	if(HAL_ADC_Start(&hadc2) == HAL_OK){	
		if(HAL_ADC_PollForConversion(&hadc2, 2) == HAL_OK){
			result = HAL_ADC_GetValue(&hadc2);
		}
	}
	osThreadYield();
	}

}



